package org.sport.repository;

import org.sport.model.entityModel.UserinfohistorytableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;


@Repository
public interface UserinfohistoryRepository extends JpaRepository<UserinfohistorytableEntity,Integer> {


    /**
     * 查询一个User的所有UserPri
     * */
    @Transactional
    @Query("select userinfohis from UserinfohistorytableEntity userinfohis where userinfohis.usertableByUserId.id =?1 order by userinfohis.id desc")
    public Page<UserinfohistorytableEntity> findLastUserPriByUserId(Integer userId,Pageable pageable);

/**
 * 查询一个User的所有UserPri
 * */
    @Transactional
    @Query("select userinfohis from UserinfohistorytableEntity userinfohis where userinfohis.usertableByUserId.id =?1")
    public List<UserinfohistorytableEntity> findAllUserPriByUserId(Integer userId);


    @Transactional
    @Query("select userinfohis from UserinfohistorytableEntity userinfohis where userinfohis.usertableByUserId.id =?1 and userinfohis.time = ?2")
    public UserinfohistorytableEntity findUserPriByUserIdAndTime(Integer userId , Timestamp qTime);


    @Modifying
    @Transactional
    @Query("update UserinfohistorytableEntity userinfohis set " +
            "userinfohis.weight=:qWeight, " +
            "userinfohis.bodyFatRate=:qBodyFatRate, " +
            "userinfohis.otherData=:qOtherData" +
            " where userinfohis.usertableByUserId=:qUserTableByUserId")
    public void updateUserInfoHis(@Param("qWeight") Double weight,
                                      @Param("qBodyFatRate") Double bodyFatRate,
                                      @Param("qOtherData") String otherData,
                                      @Param("qUserTableByUserId") UsertableEntity UsertableByUserId
    );
}
